<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>视频上传测试 - Qwen3-VL模型</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .upload-section, .test-section {
            margin-bottom: 30px;
            padding: 20px;
            border: 2px dashed #ddd;
            border-radius: 8px;
            background-color: #fafafa;
        }
        .upload-section h2, .test-section h2 {
            color: #555;
            margin-bottom: 15px;
        }
        input[type="file"], input[type="text"], textarea, select {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
        }
        button {
            background-color: #007bff;
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            margin: 10px 5px 10px 0;
        }
        button:hover {
            background-color: #0056b3;
        }
        button:disabled {
            background-color: #ccc;
            cursor: not-allowed;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 5px;
            white-space: pre-wrap;
            font-family: monospace;
            font-size: 14px;
        }
        .success {
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .error {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .info {
            background-color: #d1ecf1;
            border: 1px solid #bee5eb;
            color: #0c5460;
        }
        .video-info {
            background-color: #fff3cd;
            border: 1px solid #ffeaa7;
            color: #856404;
            padding: 15px;
            border-radius: 5px;
            margin: 10px 0;
        }
        .loading {
            display: none;
            text-align: center;
            color: #666;
        }
        .step {
            margin: 20px 0;
            padding: 15px;
            background-color: #e9ecef;
            border-left: 4px solid #007bff;
            border-radius: 0 5px 5px 0;
        }
        .step h3 {
            margin: 0 0 10px 0;
            color: #007bff;
        }
        
        .nav-bar {
            background: #f8f9fa;
            padding: 15px 20px;
            border-bottom: 1px solid #e9ecef;
            display: flex;
            gap: 15px;
            align-items: center;
            margin-bottom: 20px;
        }
        
        .nav-link {
            color: #495057;
            text-decoration: none;
            padding: 8px 16px;
            border-radius: 6px;
            font-weight: 500;
            transition: all 0.3s ease;
        }
        
        .nav-link:hover {
            background: #e9ecef;
            color: #007bff;
        }
        
        .nav-link.active {
            background: #007bff;
            color: white;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="nav-bar">
            <a href="/" class="nav-link">🏠 视频分析</a>
            <a href="/test" class="nav-link active">🧪 测试上传</a>
            <a href="/videos" class="nav-link">📹 视频管理</a>
            <a href="/large-analysis" class="nav-link">🎞️ 大视频分析</a>
        </div>
        
        <h1>🎬 视频上传测试 - Qwen3-VL模型</h1>
        <div class="info" style="background-color: #e7f3ff; padding: 15px; border-radius: 5px; margin-bottom: 20px;">
            <h3>🤖 模型信息</h3>
            <p><strong>主要模型：</strong>Qwen3-VL (本地部署)</p>
            <p><strong>备用模型：</strong>InternVL (云端API)</p>
            <p><strong>分析方式：</strong>通过视频URL直接分析，无需上传base64数据</p>
        </div>
        
        <div class="step">
            <h3>步骤1: 上传视频文件</h3>
            <p>选择一个视频文件上传到服务器，系统会为其生成一个可访问的URL</p>
        </div>
        
        <div class="upload-section">
            <h2>📁 视频上传</h2>
            <input type="file" id="videoFile" accept="video/*">
            <button onclick="uploadVideo()">上传视频</button>
            <div class="loading" id="uploadLoading">正在上传视频...</div>
            <div id="uploadResult"></div>
        </div>

        <div class="step">
            <h3>步骤2: 测试Qwen3-VL模型</h3>
            <p>使用生成的视频URL测试本地部署的Qwen3-VL模型是否能够正常处理</p>
        </div>
        
        <div class="test-section">
            <h2>🤖 Qwen3-VL模型测试</h2>
            <label for="videoUrl">视频URL:</label>
            <input type="text" id="videoUrl" placeholder="http://192.168.200.19:18080/视频文件名.mp4 或从上传结果复制">
            
            <label for="prompt">分析提示词:</label>
            <select id="promptTemplate" onchange="updatePrompt()">
                <option value="custom">自定义</option>
                <option value="step_analysis" selected>详述视频中的操作步骤，带上时间</option>
                <option value="general">请详细描述这个视频的内容，包括场景、人物、动作和主要事件</option>
                <option value="action_recognition">请识别和描述视频中发生的所有动作和行为</option>
            </select>
            <textarea id="prompt" rows="3">详述视频中的操作步骤，带上时间</textarea>
            
            <button onclick="testQwenModel()">测试Qwen3-VL模型</button>
            <div class="loading" id="testLoading">正在调用Qwen3-VL模型...</div>
            <div id="testResult"></div>
        </div>
    </div>

    <script>
        // 更新提示词
        function updatePrompt() {
            const template = document.getElementById('promptTemplate').value;
            const promptTextarea = document.getElementById('prompt');
            
            const templates = {
                'step_analysis': '详述视频中的操作步骤，带上时间',
                'general': '请详细描述这个视频的内容，包括场景、人物、动作和主要事件',
                'action_recognition': '请识别和描述视频中发生的所有动作和行为'
            };
            
            if (template !== 'custom' && templates[template]) {
                promptTextarea.value = templates[template];
            }
        }

        // 上传视频
        async function uploadVideo() {
            const fileInput = document.getElementById('videoFile');
            const file = fileInput.files[0];
            
            if (!file) {
                showResult('uploadResult', '请选择一个视频文件', 'error');
                return;
            }
            
            const loading = document.getElementById('uploadLoading');
            const resultDiv = document.getElementById('uploadResult');
            
            loading.style.display = 'block';
            resultDiv.innerHTML = '';
            
            try {
                const formData = new FormData();
                formData.append('file', file);
                
                const response = await fetch('/api/upload', {
                    method: 'POST',
                    body: formData
                });
                
                const result = await response.json();
                
                if (result.success) {
                    const videoInfo = `
                        <div class="video-info">
                            <h4>✅ 视频上传成功！</h4>
                            <p><strong>原始文件名:</strong> ${result.data.original_name}</p>
                            <p><strong>服务器文件名:</strong> ${result.data.filename}</p>
                            <p><strong>文件大小:</strong> ${(result.data.file_size / 1024 / 1024).toFixed(2)} MB</p>
                            <p><strong>访问URL:</strong> <code>${result.data.video_url}</code></p>
                            <button onclick="copyToClipboard('${result.data.video_url}')">复制URL</button>
                            <button onclick="useThisUrl('${result.data.video_url}')">使用此URL测试</button>
                        </div>
                    `;
                    showResult('uploadResult', videoInfo, 'success');
                } else {
                    showResult('uploadResult', `上传失败: ${result.message}`, 'error');
                }
            } catch (error) {
                showResult('uploadResult', `上传失败: ${error.message}`, 'error');
            } finally {
                loading.style.display = 'none';
            }
        }

        // 测试Qwen3-VL模型
        async function testQwenModel() {
            const videoUrl = document.getElementById('videoUrl').value.trim();
            const prompt = document.getElementById('prompt').value.trim();
            
            if (!videoUrl) {
                showResult('testResult', '请输入视频URL', 'error');
                return;
            }
            
            if (!prompt) {
                showResult('testResult', '请输入分析提示词', 'error');
                return;
            }
            
            const loading = document.getElementById('testLoading');
            const resultDiv = document.getElementById('testResult');
            
            loading.style.display = 'block';
            resultDiv.innerHTML = '';
            
            try {
                const formData = new FormData();
                formData.append('video_url', videoUrl);
                formData.append('prompt', prompt);
                
                const response = await fetch('/api/test-qwen', {
                    method: 'POST',
                    body: formData
                });
                
                const result = await response.json();
                
                if (result.success) {
                    const testInfo = `
                        <h4>✅ Qwen3-VL模型调用成功！</h4>
                        <p><strong>视频URL:</strong> ${result.video_url}</p>
                        <p><strong>提示词:</strong> ${result.prompt}</p>
                        <h4>📝 分析结果:</h4>
                        <div style="background: #f8f9fa; padding: 15px; border-radius: 5px; margin: 10px 0;">
                            ${JSON.stringify(result.result, null, 2)}
                        </div>
                    `;
                    showResult('testResult', testInfo, 'success');
                } else {
                    showResult('testResult', `模型调用失败: ${result.message}`, 'error');
                }
            } catch (error) {
                showResult('testResult', `模型调用失败: ${error.message}`, 'error');
            } finally {
                loading.style.display = 'none';
            }
        }

        // 显示结果
        function showResult(elementId, message, type) {
            const element = document.getElementById(elementId);
            element.innerHTML = `<div class="result ${type}">${message}</div>`;
        }

        // 复制到剪贴板
        function copyToClipboard(text) {
            navigator.clipboard.writeText(text).then(() => {
                alert('URL已复制到剪贴板');
            });
        }

        // 使用此URL进行测试
        function useThisUrl(url) {
            document.getElementById('videoUrl').value = url;
            alert('URL已填入测试区域');
        }
    </script>
</body>
</html>